package com.cx.java;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cx.cx.pojo.Car;
import cx.cx.pojo.User;

/**
 * Servlet implementation class CarServlet
 */
@WebServlet("/CarServlet")
public class CarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
JdbcUtils ju = JdbcUtils.getInstance();

String name= request.getParameter("username");
String pwd= request.getParameter("pwd");
String address= request.getParameter("address");
String sql = "select * from testdb where username='"+name+"'and pwd='"+pwd+"'";
List<User> user= ju.executeQuery(User.class,sql);

if(!user.isEmpty()) {
	
 

		//后台测试
		//建立会话，会话是为了保证待跳转界面可以获取隐式对象session
		HttpSession session=request.getSession();
		session.setAttribute("name",name);
		//请求转发，url不会改变，实质是服务器端响应forward(request,reponse)发送到浏览器，而不是浏览器在操作
		//例如：地址为“localhost:8080/car/CarServlet”，回车后界面会跳转到car.jsp界面，但是url仍然是“localhost:8080/car/CarServlet”
		request.getRequestDispatcher("/home.jsp").forward(request,response);
		//response.sendRedirect("/car/home.jsp");
}
else {
	response.sendRedirect("/car/land.html");
}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
